package net.tp.algo.graph;

import java.util.*;
import net.tp.util.Wrappers;

/**
 *
 * @author Trung Phan
 */
public class UndirectedGraph extends BaseGraph {

    private int size;
    private List<Edge> edges = new LinkedList<>();

    public UndirectedGraph(int order) {
        super(order);
    }

    public UndirectedGraph addEdge(int u, int w) {
        return addEdge(u, w, 1);
    }

    public UndirectedGraph addEdge(int u, int w, double weight) {
        Edge edge = new EdgeImpl(u, w, weight);

        if (nodes[u] == null) {
            nodes[u] = new LinkedList<>();
        }
        nodes[u].add(edge);

        if (nodes[w] == null) {
            nodes[w] = new LinkedList<>();
        }
        nodes[w].add(edge);
        size++;
        
        this.edges.add(edge);

        return this;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public Iterable<Edge> edges() {
        return Wrappers.makeIterable(this.edges);
    }
}
